package cj.web.marry.utils;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ImagePicture {
	
	public static void main(String[] args) throws IOException{
		BufferedImage bi = ImageIO.read(new File("c:/template.jpg"));

		BufferedImage newsImage = rotateImage(bi);
		ImageIO.write(newsImage, "png", new File("c:/test.png"));  
	}
	
	/**
     * 旋转图片为指定角度
     * 
     * @param bufferedimage
     *            目标图像
     * @param degree
     *            旋转角度
     * @return
     */
    public static BufferedImage rotateImage(final BufferedImage bufferedimage) {
    	
    	int degree = 90;
    	
        int w = bufferedimage.getHeight();
        int h = bufferedimage.getWidth();
        
        int type = bufferedimage.getColorModel().getTransparency();
        BufferedImage img;
        Graphics2D graphics2d;
        (graphics2d = (img = new BufferedImage(w, h, type))
                .createGraphics()).setRenderingHint(
                RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);
        
        // 确定原点坐标
        int x = (w / 2) - (h / 2);
		int y = (h / 2) - (w / 2);
        
        graphics2d.drawImage(bufferedimage, x, y, null);
        graphics2d.dispose();
        return img;
    }
}